package com.qh.resourcehub.program.virtual.dao.Impl;

import com.qh.resourcehub.program.dto.VirtualDto;
import com.qh.resourcehub.program.virtual.dao.VirtualDao;
import com.qh.resourcehub.utils.XuguUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

@Repository("VirtualDao")
public class VirtualDaoImpl implements VirtualDao {
    @Autowired
    private XuguUtils xuguUtils;

    @Override
    public int ApplicationSave(VirtualDto virtualDto) {
        int result = 0;
        Connection connection = null;
        PreparedStatement pstmtUser = null;
        try {
            connection = xuguUtils.getConnection();
            // 新增操作
            StringBuilder sqlUser = new StringBuilder("INSERT INTO APPROVE_INFO_VIRTUAL (");
            StringBuilder placeholders = new StringBuilder(" VALUES (");
            List<Object> values = new ArrayList<>();
            if (!StringUtils.isEmpty(virtualDto.getInfoId() )) {
                sqlUser.append("INFO_ID, ");
                placeholders.append("?, ");
                values.add(virtualDto.getInfoId());
            }
            if (!StringUtils.isEmpty(virtualDto.getProcessStatus())) {
                sqlUser.append("PROCESS_STATUS, ");
                placeholders.append("?, ");
                values.add(virtualDto.getProcessStatus());
            }
            if (!StringUtils.isEmpty(virtualDto.getApplicantUser() )) {
                sqlUser.append("APPLICANT_USER, ");
                placeholders.append("?, ");
                values.add(virtualDto.getApplicantUser());
            }
            if (!StringUtils.isEmpty(virtualDto.getSystemSecurityOfficer() )) {
                sqlUser.append("SYSTEM_SECURITY_OFFICER, ");
                placeholders.append("?, ");
                values.add(virtualDto.getSystemSecurityOfficer());
            }
            if (!StringUtils.isEmpty(virtualDto.getContactInfo())) {
                sqlUser.append("CONTACT_INFO, ");
                placeholders.append("?, ");
                values.add(virtualDto.getContactInfo());
            }
            if (!StringUtils.isEmpty(virtualDto.getSystemName())) {
                sqlUser.append("SYSTEM_NAME, ");
                placeholders.append("?, ");
                values.add(virtualDto.getSystemName());
            }

            if (virtualDto.getApplicantUnitDepartment() != null) {
                sqlUser.append("APPLICANT_UNITDEPARTMENT, ");
                placeholders.append("?, ");
                values.add(virtualDto.getApplicantUnitDepartment());
            }
            if (virtualDto.getApplyUser() != null) {
                sqlUser.append("APPLY_USER, ");
                placeholders.append("?, ");
                values.add(virtualDto.getApplyUser());
            }
            if (virtualDto.getApplyArea() != null) {
                sqlUser.append("APPLIY_AREA, ");
                placeholders.append("?, ");
                values.add(virtualDto.getApplyArea());
            }

            if (values.isEmpty()) {
                return result; // 如果没有任何字段，直接返回
            }

            sqlUser.setLength(sqlUser.length() - 2); // 去掉最后的逗号和空格
            placeholders.setLength(placeholders.length() - 2); // 去掉最后的逗号和空格

            sqlUser.append(")").append(placeholders).append(")");

            pstmtUser = connection.prepareStatement(sqlUser.toString());

            // 设置参数
            for (int i = 0; i < values.size(); i++) {
                if (values.get(i) instanceof Integer) {
                    pstmtUser.setInt(i + 1, (Integer) values.get(i));
                } else if (values.get(i) instanceof Timestamp) {
                    pstmtUser.setTimestamp(i + 1, (Timestamp) values.get(i));
                } else if (values.get(i) instanceof Time) {
                    pstmtUser.setTime(i + 1, (Time) values.get(i));
                }  else {
                    pstmtUser.setObject(i + 1, values.get(i));
                }
            }

            System.out.println("Generated SQL: " + sqlUser);
            System.out.println("Parameters: " + values);
            result = pstmtUser.executeUpdate();  // 执行新增操作
            return result;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 确保资源关闭
            if (pstmtUser != null) {
                try {
                    pstmtUser.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return 0;
    }
}
